package com.weic.easycache.http.proxy;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class ProxyService extends Service {
	private static final String LOG_TAG = "ProxyService";
	private static final boolean LOG = true;

	HttpProxyServer mProxyServer;
	IBinder mBinder;

	@Override
	public void onCreate() {
		super.onCreate();
		if(LOG) Log.i(LOG_TAG, "onCreate--");

		if(LOG) Log.i(LOG_TAG, "ProcessId:"+android.os.Process.myPid());

		mProxyServer = new HttpProxyServer();
		mBinder = new IHtpsBridge.Stub() {

			@Override
			public void start(boolean daemon) throws RemoteException {
				mProxyServer.start(daemon);
			}

			@Override
			public void stop() throws RemoteException {
				mProxyServer.stop();
			}

			@Override
			public boolean isAlive() throws RemoteException {
				return mProxyServer.isAlive();
			}

			@Override
			public int getProxyPort() throws RemoteException {
				return mProxyServer.getProxyPort();
			}

			@Override
			public String getProxyHostAdress() throws RemoteException {
				return mProxyServer.getProxyHostAdress();
			}

			@Override
			public String getProxyHost() throws RemoteException {
				return mProxyServer.getProxyHost();
			}
		};
	}

	@Override
	public IBinder onBind(Intent intent) {
		if(LOG) Log.i(LOG_TAG, "onBind--");
		return mBinder;
	}
	@Override
	public void onRebind(Intent intent) {
		super.onRebind(intent);
		if(LOG) Log.i(LOG_TAG, "onRebind--");
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if(LOG) Log.i(LOG_TAG, "onStartCommand--");
		return super.onStartCommand(intent, flags, startId);
	}
	@Override
	public boolean onUnbind(Intent intent) {
		if(LOG) Log.i(LOG_TAG, "onUnbind--");
		mProxyServer.stop();
		return super.onUnbind(intent);
	}
	@Override
	public void onDestroy() {
		if(LOG) Log.i(LOG_TAG, "onDestroy--");
		super.onDestroy();
	}
}
